<template lang="pug">
.material
  .material-header
    .material-header-search
      el-input(placeholder="材料名称/发布者" prefix-icon="el-icon-search" clearable v-model="searchString" @change="changeSearch")
      el-select(placeholder="选择材料品牌" prefix-icon="el-icon-search" clearable v-model="brandId" @change="changeBrand")
        el-option(v-for="item in brandList", :label="item.name", :value="item.id", :key="item.id")
    .material-header-info 设置热门材料，最多设置7个材料
  .material-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      empty-text="未找到符合条件的内容"
      :cell-style="cellStyle"
    )
      el-table-column(type="index" width="55" align="center" label="序号")
      el-table-column(label="材料名称" prop="materialName" min-width="100")
        template(slot-scope="scope")
          router-link.table-item-bd(:to="{ name: 'hotMaterialDetail', params: { id: scope.row.materialId } }")
            .table-item-case-name {{scope.row.materialName}}
      el-table-column(label="分类名称" prop="classificationName")
      el-table-column(label="发布者" prop="companyName")
      el-table-column(label="品牌" prop="brandName")
      el-table-column(label="市场价" sortable align="right", sort-by="highPrice")
        template(slot-scope="scope") ￥{{scope.row.lowPrice}}～{{scope.row.highPrice}}
      el-table-column(label="浏览量" sortable align="right" prop="viewAmount")
      el-table-column(label="创建时间" sortable prop="createTime")
        template(slot-scope="scope") {{new Date(scope.row.createTime).format('yyyy-MM-dd')}}
      el-table-column(label="操作" v-if="$permission('AB-01006-000')")
        template(slot-scope="scope")
          .table-item-handle
            el-switch(
              v-model="scope.row.isHot"
              active-color="#8676FF"
              inactive-color="#B2B4C7"
              @change="changeClassCase(scope.row)"
              :disabled="scope.row.isDisable"
            )
  .table-pagination
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    )
</template>

<script>
import { getBrandList, getMaterialList, setHotMaterial, } from 'api'
export default {
  name: 'hotMaterialList',
  data () {
    return {
      loading: false,
      total: 0,
      page: 1,
      pageSize: 20,
      searchString: '',
      brandId: '',
      list: [],
      brandList: []
    }
  },
  computed: {
    maxNum () {
      let len = this.list.filter(item => item.isHot).length
      if (len >= 7) {
        this.list.forEach(item => {
          if (!item.isHot) item.isDisable = true
          else item.isDisable = false
        })
      } else {
        this.list.forEach(item => {
          item.isDisable = false
        })
      }
      return len
    }
  },
  methods: {
    async changeClassCase (item) {
      try {
        if (this.maxNum > 7) {
          this.$message.error('经典案例最多设置7个！')
          return
        }
        await this.$axios.post(setHotMaterial, { materialId: item.materialId, isHot: item.isHot })
        this.$message.success('设置成功！')
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    cellStyle ({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 0 && row.isHot) {
        return { background: '#FFF5F8' }
      }
    },
    currentChange (page) {
      this.page = page
      this.getList()
    },
    changeSearch () {
      this.page = 1
      this.getList()
    },
    changeBrand () {
      this.page = 1
      this.getList()
    },
    async getList () {
      try {
        this.loading = true
        await this.getBrandList()
        let query = { ...this.$route.query, currentPage: this.page, pageLines: this.pageSize, status: 2 }
        if (this.searchString) query.searchString = this.searchString
        if (this.brandId) query.brandId = this.brandId
        const { ResultSet } = await this.$axios.post(getMaterialList, { ...query })
        let len = ResultSet.userData.result.filter(item => item.isHot).length
        if (len >= 7) {
          ResultSet.userData.result.forEach(item => {
            if (!item.isHot) item.isDisable = true
            else item.isDisable = false
          })
        }
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.result
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    },
    async getBrandList () {
      try {
        const { ResultSet } = await this.$axios.post(getBrandList)
        this.brandList = ResultSet.userData.loadBrandList
      } catch (err) {
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(async vm => {
      // await vm.getBrandList()
      await vm.getList()
    })
  },
  async beforeRouteUpdate (to, from, next) {
    next()
    // await this.getBrandList()
    await this.getList()
  }
} 
</script>

<style lang="scss" scoped>
.material {
  width: 100%;
  height: 100%;
  background: #FFFFFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.material-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  .el-input {
    width: 202px;
    margin-right: 16px;
  }
}
.material-table {
  height: calc(100% - 150px);
}
.material-header-search {
  display: flex;
  align-items: center;
}
.material-header-info {
  font-size: 14px;
  line-height: 22px;
  color: #8B8EAB;
}
.table-item-case-name {
  color: #FF2E42;
}
.table-item-header {
  display: flex;
  align-items: center;
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
</style>
